import errorFun from '../../util/errorFun';
import requestTimeOut from '../../util/requestTimeout';

Page({
    data: {
        musicList: [],
        playingIndex: 0,
        playStatus: false,
        playMode: 0,    // 0 顺序   1 单曲循环  2 随机播放
        showList: false,
        name: '',
        author: '',
        poster: '',
        currentTime: '00:00',
        durationTime: '00:00',
        percentVal: 0,
        duration: 0,
        dvdNowIndex: null,
        dvdNowList: []
    },
    onLoad (options) {
        this.backgroundAudioManager = wx.getBackgroundAudioManager();
        if (options.nowDvdList && options.nowDvdList != undefined) {
            let newNowDvdList = JSON.parse(options.nowDvdList)
            this.setData({
                dvdNowIndex: options.nowIndex,
                dvdNowList: newNowDvdList
            })
        } else {
            requestTimeOut();
        }
        this.initMusic(this.data.dvdNowIndex, this.data.dvdNowList.list);
        this.backgroundAudioManager.onEnded(() => {
            this.natureOverplayModeFun();
        })
        this.backgroundAudioManager.onError((err) => {
            errorFun(err);
        })
        this.backgroundAudioManager.onCanplay(() => {
            console.log('进入播放状态');
        })
        this.backgroundAudioManager.onTimeUpdate(() => {
            this.setData({
                percentVal: Math.ceil(this.backgroundAudioManager.currentTime),
                duration: Math.ceil(this.backgroundAudioManager.duration),
                currentTime: this.formatTime(Math.floor(this.backgroundAudioManager.currentTime)),
                durationTime: this.formatTime(Math.floor(this.backgroundAudioManager.duration))
            })
        })
    },
    initMusic (dvdNIndex, dvdNList) {
        wx.showLoading({
            title: '加载中',
            mask: true,
        })
        let timer = setTimeout(() => {
            requestTimeOut();
        },10000)
        if (dvdNList.length > 0) {
            wx.hideLoading();
            clearTimeout(timer);
            this.setData({
                musicList: dvdNList
            })
            this.setPlayingMusic(dvdNIndex);
        } else {
            wx.showToast({
                title: '没有获取到数据',
                duration: 2000,
                mask: true
            })
        }
    },
    natureOverplayModeFun () {
        if (this.data.playMode === 0) {
            this.bindNextPrevFun();
            this.nextMusicStatus();
        } else if (this.data.playMode === 1) {
            let newIndex = this.data.playingIndex < this.data.musicList.length - 1 ? this.data.playingIndex : 0;
            this.setPlayingMusic(newIndex);
            this.nextMusicStatus();
        } else {
            this.randomPlayModeFun();
        }
    },
    randomPlayModeFun () {
        let newAudioIndex = Math.floor(Math.random() * this.data.musicList.length);
        while (newAudioIndex === this.data.playingIndex) {
            newAudioIndex = Math.floor(Math.random() * this.data.musicList.length);
        }
        let newIndex = this.data.playingIndex < this.data.musicList.length - 1 ? newAudioIndex : 0;
        this.setPlayingMusic(newIndex);
        this.nextMusicStatus();
    },
    bindNextPrevFun (evt) {
        if (this.data.playMode === 2) {
            this.randomPlayModeFun();
        } else {
            let nextIndex = this.data.playingIndex > this.data.musicList.length - 1 ? 0 : Number(this.data.playingIndex) + Number(evt.currentTarget.dataset.nextprev);
            if (this.data.playingIndex < 0) {
                nextIndex = this.data.musicList.length - 1;
            }else{
                // 不需要处理
            }
            this.nextMusicStatus();
            this.setPlayingMusic(nextIndex);
        }
    },
    setPlayingMusic (index) {
        let newIndex = this.data.musicList[index];
        this.backgroundAudioManager.src = encodeURI(newIndex.src);
        this.backgroundAudioManager.title = newIndex.name
            this.setData({
                playStatus: true
            })
        this.setData({
            percentVal: 0,
            playingIndex: index,
            name: newIndex.name,
            poster: newIndex.poster,
            author: newIndex.author,
            currentTime: '00:00',
            durationTime: '00:00',
        })
    },
    bindPlayFun () {
        if (!this.data.playStatus) {
            this.backgroundAudioManager.play();
            this.setData({
                playStatus: true,
            })
        } else {
            this.backgroundAudioManager.pause();
            this.setData({
                playStatus: false,
            })
        }
    },
    sliderChange (evt) {
        this.nextMusicStatus();
        this.backgroundAudioManager.seek(evt.detail.value);
        this.setData({
            currentTime: this.formatTime(evt.detail.value),
        })
    },
    orderMusic () {
        if (this.data.playMode + 1 > 2) {
            this.setData({
                playMode: 0,
            })
        } else {
            this.setData({
                playMode: this.data.playMode + 1,
            })
        }
    },
    nextMusicStatus () {
        if (this.data.playStatus === false) {
            this.setData({
                playStatus: false,
            }),
            this.backgroundAudioManager.pause();
        } else {
            this.setData({
                playStatus: true,
            }),
            this.backgroundAudioManager.play();
        }
    },
    formatTime (time) {
        let minute = Math.floor(time / 60) % 60;
        let second = Math.floor(time) % 60;
        let newMin = minute < 10 ? '0' + minute : minute;
        let newSec = second < 10 ? '0' + second : second;
        return `${newMin}:${newSec}`;
    },
    bindShowList () {
        this.setData({
            showList: true,
        })
    },
    bindListClick (evt) {
        let newIndex = evt.currentTarget.dataset.index;
        this.setPlayingMusic(newIndex);
        this.backgroundAudioManager.play();
        this.setData({
            playStatus: true,
        })
    },
    hideView () {
        this.setData({
            showList: false,
        })
    },
})
